#ifndef THINK_THINK_COOKIE_H
#define THINK_THINK_COOKIE_H

static char* think_cookie =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"class Cookie\n"
	"{\n"
	"    /**\n"
	"     * @var array cookie 设置参数\n"
	"     */\n"
	"    protected static $config = [\n"
	"        \'prefix\'    => \'\', // cookie 名称前缀\n"
	"        \'expire\'    => 0, // cookie 保存时间\n"
	"        \'path\'      => \'/\', // cookie 保存路径\n"
	"        \'domain\'    => \'\', // cookie 有效域名\n"
	"        \'secure\'    => false, //  cookie 启用安全传输\n"
	"        \'httponly\'  => false, // httponly 设置\n"
	"        \'setcookie\' => true, // 是否使用 setcookie\n"
	"    ];\n"
	"\n"
	"    /**\n"
	"     * @var bool 是否完成初始化了\n"
	"     */\n"
	"    protected static $init;\n"
	"\n"
	"    /**\n"
	"     * Cookie初始化\n"
	"     * @access public\n"
	"     * @param  array $config 配置参数\n"
	"     * @return void\n"
	"     */\n"
	"    public static function init(array $config = [])\n"
	"    {\n"
	"        if (empty($config)) {\n"
	"            $config = Config::get(\'cookie\');\n"
	"        }\n"
	"\n"
	"        self::$config = array_merge(self::$config, array_change_key_case($config));\n"
	"\n"
	"        if (!empty(self::$config[\'httponly\'])) {\n"
	"            ini_set(\'session.cookie_httponly\', 1);\n"
	"        }\n"
	"\n"
	"        self::$init = true;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 设置或者获取 cookie 作用域（前缀）\n"
	"     * @access public\n"
	"     * @param  string $prefix 前缀\n"
	"     * @return string|\n"
	"     */\n"
	"    public static function prefix($prefix = \'\')\n"
	"    {\n"
	"        if (empty($prefix)) {\n"
	"            return self::$config[\'prefix\'];\n"
	"        }\n"
	"\n"
	"        return self::$config[\'prefix\'] = $prefix;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * Cookie 设置、获取、删除\n"
	"     * @access public\n"
	"     * @param  string $name   cookie 名称\n"
	"     * @param  mixed  $value  cookie 值\n"
	"     * @param  mixed  $option 可选参数 可能会是 null|integer|string\n"
	"     * @return void\n"
	"     */\n"
	"    public static function set($name, $value = \'\', $option = null)\n"
	"    {\n"
	"        !isset(self::$init) && self::init();\n"
	"\n"
	"        // 参数设置(会覆盖黙认设置)\n"
	"        if (!is_null($option)) {\n"
	"            if (is_numeric($option)) {\n"
	"                $option = [\'expire\' => $option];\n"
	"            } elseif (is_string($option)) {\n"
	"                parse_str($option, $option);\n"
	"            }\n"
	"\n"
	"            $config = array_merge(self::$config, array_change_key_case($option));\n"
	"        } else {\n"
	"            $config = self::$config;\n"
	"        }\n"
	"\n"
	"        $name = $config[\'prefix\'] . $name;\n"
	"\n"
	"        // 设置 cookie\n"
	"        if (is_array($value)) {\n"
	"            array_walk_recursive($value, \'self::jsonFormatProtect\', \'encode\');\n"
	"            $value = \'think:\' . json_encode($value);\n"
	"        }\n"
	"\n"
	"        $expire = !empty($config[\'expire\']) ?\n"
	"        $_SERVER[\'REQUEST_TIME\'] + intval($config[\'expire\']) :\n"
	"        0;\n"
	"\n"
	"        if ($config[\'setcookie\']) {\n"
	"            setcookie(\n"
	"                $name, $value, $expire, $config[\'path\'], $config[\'domain\'],\n"
	"                $config[\'secure\'], $config[\'httponly\']\n"
	"            );\n"
	"        }\n"
	"\n"
	"        $_COOKIE[$name] = $value;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 永久保存 Cookie 数据\n"
	"     * @access public\n"
	"     * @param  string $name   cookie 名称\n"
	"     * @param  mixed  $value  cookie 值\n"
	"     * @param  mixed  $option 可选参数 可能会是 null|integer|string\n"
	"     * @return void\n"
	"     */\n"
	"    public static function forever($name, $value = \'\', $option = null)\n"
	"    {\n"
	"        if (is_null($option) || is_numeric($option)) {\n"
	"            $option = [];\n"
	"        }\n"
	"\n"
	"        $option[\'expire\'] = 315360000;\n"
	"\n"
	"        self::set($name, $value, $option);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 判断是否有 Cookie 数据\n"
	"     * @access public\n"
	"     * @param  string      $name   cookie 名称\n"
	"     * @param  string|null $prefix cookie 前缀\n"
	"     * @return bool\n"
	"     */\n"
	"    public static function has($name, $prefix = null)\n"
	"    {\n"
	"        !isset(self::$init) && self::init();\n"
	"\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$config[\'prefix\'];\n"
	"\n"
	"        return isset($_COOKIE[$prefix . $name]);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取 Cookie 的值\n"
	"     * @access public\n"
	"     * @param string      $name   cookie 名称\n"
	"     * @param string|null $prefix cookie 前缀\n"
	"     * @return mixed\n"
	"     */\n"
	"    public static function get($name = \'\', $prefix = null)\n"
	"    {\n"
	"        !isset(self::$init) && self::init();\n"
	"\n"
	"        $prefix = !is_null($prefix) ? $prefix : self::$config[\'prefix\'];\n"
	"        $key    = $prefix . $name;\n"
	"\n"
	"        if (\'\' == $name) {\n"
	"            // 获取全部\n"
	"            if ($prefix) {\n"
	"                $value = [];\n"
	"\n"
	"                foreach ($_COOKIE as $k => $val) {\n"
	"                    if (0 === strpos($k, $prefix)) {\n"
	"                        $value[$k] = $val;\n"
	"                    }\n"
	"\n"
	"                }\n"
	"            } else {\n"
	"                $value = $_COOKIE;\n"
	"            }\n"
	"        } elseif (isset($_COOKIE[$key])) {\n"
	"            $value = $_COOKIE[$key];\n"
	"\n"
	"            if (0 === strpos($value, \'think:\')) {\n"
	"                $value = json_decode(substr($value, 6), true);\n"
	"                array_walk_recursive($value, \'self::jsonFormatProtect\', \'decode\');\n"
	"            }\n"
	"        } else {\n"
	"            $value = null;\n"
	"        }\n"
	"\n"
	"        return $value;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 删除 Cookie\n"
	"     * @access public\n"
	"     * @param  string      $name   cookie 名称\n"
	"     * @param  string|null $prefix cookie 前缀\n"
	"     * @return void\n"
	"     */\n"
	"    public static function delete($name, $prefix = null)\n"
	"    {\n"
	"        !isset(self::$init) && self::init();\n"
	"\n"
	"        $config = self::$config;\n"
	"        $prefix = !is_null($prefix) ? $prefix : $config[\'prefix\'];\n"
	"        $name   = $prefix . $name;\n"
	"\n"
	"        if ($config[\'setcookie\']) {\n"
	"            setcookie(\n"
	"                $name, \'\', $_SERVER[\'REQUEST_TIME\'] - 3600, $config[\'path\'],\n"
	"                $config[\'domain\'], $config[\'secure\'], $config[\'httponly\']\n"
	"            );\n"
	"        }\n"
	"\n"
	"        // 删除指定 cookie\n"
	"        unset($_COOKIE[$name]);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 清除指定前缀的所有 cookie\n"
	"     * @access public\n"
	"     * @param  string|null $prefix cookie 前缀\n"
	"     * @return void\n"
	"     */\n"
	"    public static function clear($prefix = null)\n"
	"    {\n"
	"        if (empty($_COOKIE)) {\n"
	"            return;\n"
	"        }\n"
	"\n"
	"        !isset(self::$init) && self::init();\n"
	"\n"
	"        // 要删除的 cookie 前缀，不指定则删除 config 设置的指定前缀\n"
	"        $config = self::$config;\n"
	"        $prefix = !is_null($prefix) ? $prefix : $config[\'prefix\'];\n"
	"\n"
	"        if ($prefix) {\n"
	"            foreach ($_COOKIE as $key => $val) {\n"
	"                if (0 === strpos($key, $prefix)) {\n"
	"                    if ($config[\'setcookie\']) {\n"
	"                        setcookie(\n"
	"                            $key, \'\', $_SERVER[\'REQUEST_TIME\'] - 3600, $config[\'path\'],\n"
	"                            $config[\'domain\'], $config[\'secure\'], $config[\'httponly\']\n"
	"                        );\n"
	"                    }\n"
	"\n"
	"                    unset($_COOKIE[$key]);\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * json 转换时的格式保护\n"
	"     * @access protected\n"
	"     * @param  mixed  $val  要转换的值\n"
	"     * @param  string $key  键名\n"
	"     * @param  string $type 转换类别\n"
	"     * @return void\n"
	"     */\n"
	"    protected static function jsonFormatProtect(&$val, $key, $type = \'encode\')\n"
	"    {\n"
	"        if (!empty($val) && true !== $val) {\n"
	"            $val = \'decode\' == $type ? urldecode($val) : urlencode($val);\n"
	"        }\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif